import 'package:bxultimate/app/router/router_name.dart';
import 'package:bxultimate/app/theme/app_theme.dart';
import 'package:bxultimate/app/theme/gaps.dart';
import 'package:bxultimate/data/request/application_request.dart';
import 'package:bxultimate/domain/application/examination/ExaminationDto.dart';
import 'package:bxultimate/resources/assets.dart';
import 'package:bxultimate/widgets/mk_load_image.dart';
import 'package:flutter/material.dart';
import 'package:flutter_easyrefresh/easy_refresh.dart';
import 'package:get/get.dart';
import 'package:tdesign_flutter/tdesign_flutter.dart';

class ExaminationTabItemView extends StatefulWidget {
  final String status;

  const ExaminationTabItemView({super.key, required this.status});

  @override
  State<ExaminationTabItemView> createState() => _ExaminationTabItemViewState();
}

class _ExaminationTabItemViewState extends State<ExaminationTabItemView> {
  List<ExaminationDto> listData = [];

  int page = 1;
  int size = 10;

  getData() async {
    listData.addAll(
        await ApplicationRequest.getExaminationList(page, size, widget.status));
    setState(() {});
  }

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    getData();
  }

  @override
  Widget build(BuildContext context) {
    return EasyRefresh(
      header: TDRefreshHeader(),
      onRefresh: () async {
        listData = [];
        getData();
      },
      onLoad: () async {
        page++;
        getData();
      },
      child: ListView.builder(
        itemCount: listData.length,
        itemBuilder: (context, index) {
          return GestureDetector(
            onTap: () {
              if (widget.status == "2") {
                Get.toNamed(RouterName.examinationReady, arguments: {
                  "id": listData[index].id,
                  "time": listData[index].remainingTime,
                });
              } else {
                Get.toNamed(RouterName.examinationResult,
                    arguments: listData[index].id);
              }
            },
            child: Padding(
              padding:
                  EdgeInsets.symmetric(horizontal: AppTheme.iDefaultPadding),
              child: _buildExaminationItem(listData[index]),
            ),
          );
        },
      ),
    );
  }

  _buildExaminationItem(ExaminationDto examinationDto) {
    return Container(
      padding: EdgeInsets.all(AppTheme.iDefaultPadding),
      margin: EdgeInsets.only(bottom: AppTheme.iDefaultPadding),
      decoration: BoxDecoration(
        color: Colors.white,
        borderRadius: BorderRadius.circular(AppTheme.iDefaultRadius),
      ),
      child: Column(
        mainAxisAlignment: MainAxisAlignment.spaceBetween,
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          Row(
            mainAxisAlignment: MainAxisAlignment.spaceBetween,
            children: [
              Container(
                width: 50,
                height: 50,
                clipBehavior: Clip.antiAlias,
                decoration: BoxDecoration(
                  borderRadius: BorderRadius.circular(AppTheme.iDefaultRadius),
                ),
                child: widget.status == "2"
                    ? MkLoadImage(Assets.examinationWait)
                    : MkLoadImage(Assets.examinationComplete),
              ),
              Gaps.hGap8,
              Expanded(
                child: Column(
                  mainAxisAlignment: MainAxisAlignment.spaceAround,
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: [
                    Text(
                      examinationDto.title!,
                      style:
                          TextStyle(fontSize: 16, fontWeight: FontWeight.bold),
                    ),
                    Gaps.vGap4,
                    Text(
                      "截止时间" + examinationDto.time!,
                      style: TextStyle(color: Color(0xFF606266)),
                    ),
                  ],
                ),
              )
            ],
          ),
          Gaps.vGap8,
          Row(
            mainAxisAlignment: MainAxisAlignment.spaceBetween,
            children: [
              TDTag(
                "共${examinationDto.option!.split(",").length}题",
                theme: TDTagTheme.primary,
                isOutline: true,
                size: TDTagSize.small,
              ),
              Text("考试时间: ${examinationDto.remainingTime}分钟",
                  style: TextStyle(color: Color(0xFF606266), fontSize: 12)),
            ],
          )
        ],
      ),
    );
  }
}
